package org.security.filter;


import org.security.config.FreeProperties;
import org.security.localstore.UserContext;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * @author jt
 * @version 1.0
 * @date 2024/6/25 17:31
 */
public class FilterHandlerProxy implements Filter {

//    private static final Logger log = LoggerFactory.getLogger(FunctionFilter.class);
//    private static final String PARAMES = "?code=%s&msg=%s";

    private FreeProperties properties;


    private FilterHandlerManager handlerManager;


    public FilterHandlerProxy(FreeProperties properties,FilterHandlerManager handlerManager) {
        this.properties = properties;
        this.handlerManager = handlerManager;
    }



    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException {
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        try {
            request.setCharacterEncoding(StandardCharsets.UTF_8.name());
            response.setCharacterEncoding(StandardCharsets.UTF_8.name());
            // TODO: 2024/7/20 执行拦截流程
            handlerManager.exec(request,response);
            filterChain.doFilter(request, response);
        } catch (Exception e) {
            e.printStackTrace();
//            forward(request,response, properties.getErrorUrl(),e);
        }finally {
            UserContext.remove();
        }
    }


}